////Copyright 2011 - Jonathan Allen

//using System;
//using System.Linq;
//using System.Windows;
//using System.Windows.Interactivity;
//using System.Windows.Input;

//namespace Granite.Xaml.Behaviors
//{
//    /// <summary>
//    /// This behavior fires the indicated ICommand when the user double-clicks on the control. The trigger is on MouseDown of the second click.
//    /// </summary>
//    /// <remarks>Silverlight doesn't have double-click support so it uses 500 millisecond window between clicks</remarks>
//    public sealed partial class DoubleClickCommandBehavior : Behavior<UIElement>
//    {
//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//        public static readonly DependencyProperty CommandParameterProperty =
//                    DependencyProperty.Register("CommandParameter", typeof(object), typeof(DoubleClickCommandBehavior), new PropertyMetadata(null));

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(DoubleClickCommandBehavior), new PropertyMetadata(null, OnCommandChanged));

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public ICommand Command
//        {
//            get { return (ICommand)AssociatedObject.GetValue(CommandProperty); }
//            set { AssociatedObject.SetValue(CommandProperty, value); }
//        }

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public object CommandParameter
//        {
//            get { return (object)AssociatedObject.GetValue(CommandParameterProperty); }
//            set { AssociatedObject.SetValue(CommandParameterProperty, value); }
//        }

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public static ICommand GetCommand(UIElement control)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            return (ICommand)control.GetValue(CommandProperty);
//        }

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static object GetCommandParameter(UIElement control)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            return control.GetValue(CommandParameterProperty);
//        }

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public static void SetCommand(UIElement control, ICommand value)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            control.SetValue(CommandProperty, value);
//        }

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static void SetCommandParameter(UIElement control, object value)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            control.SetValue(CommandParameterProperty, value);
//        }

//        static void OnCommandChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
//        {
//            var behaviors = Interaction.GetBehaviors(control);
//            var existingBehavior = behaviors.OfType<DoubleClickCommandBehavior>().FirstOrDefault();

//            if (e.NewValue == null)
//            {
//                if (existingBehavior != null)
//                    behaviors.Remove(existingBehavior);
//            }
//            else
//            {
//                behaviors.Add(new DoubleClickCommandBehavior());
//            }
//        }

//    }

//#if WPF
//    public partial class DoubleClickCommandBehavior : Behavior<UIElement>
//    {
//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//            AssociatedObject.MouseDown += AssociatedObject_MouseDown;
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
//        }

//        void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs mouseEventArgs)
//        {
//            if (mouseEventArgs.ClickCount == 2)
//            {
//                if (Command.CanExecute(CommandParameter))
//                    Command.Execute(CommandParameter);
//            }
//        }

//    }
//#endif

//#if SILVERLIGHT
//    public partial class DoubleClickCommandBehavior : Behavior<UIElement>
//    {
//        static readonly TimeSpan DoubleClickWindow = new TimeSpan(0, 0, 0, 0, 500);
//        DateTime _lastClick = DateTime.MinValue;

//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//            AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseDown;
//            AssociatedObject.MouseRightButtonDown += AssociatedObject_MouseDown;
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//            AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseDown;
//            AssociatedObject.MouseRightButtonDown -= AssociatedObject_MouseDown;
//        }

//        void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs mouseEventArgs)
//        {
//            if ((DateTime.Now - _lastClick) < DoubleClickWindow)
//            {
//                if (Command.CanExecute(CommandParameter))
//                    Command.Execute(CommandParameter);
//            }
//            _lastClick = DateTime.Now;
//        }
//    }
//#endif

//}
